package com.obst.tracenote.util;

import org.json.JSONObject;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.obst.tracenote.C;
import com.obst.tracenote.LoginAct;
import com.obst.tracenote.MainAct;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class FacebookAPI {
	private Facebook mFacebook;
	private Activity mTargetActivity;
	private String mFacebookAccessToken;
    final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
    final static int PICK_EXISTING_PHOTO_RESULT_CODE = 1;
    private MyServer mServer;
	public FacebookAPI(Facebook fb, Activity activity) {
		mFacebook = fb;
		mTargetActivity = activity;
		mFacebookAccessToken = MyPreferences.getAppPreferences(mTargetActivity,
				"FACEBOOK_TOKEN");
		mFacebook.setAccessToken(mFacebookAccessToken);
	}

	public void login() {
		if (!"".equals(mFacebookAccessToken) && mFacebookAccessToken != null) {
			mFacebook.setAccessToken(mFacebookAccessToken);
		} else {
			Log.v(C.LOG_TAG, "login start");
			/*
			mFacebook.authorize(
					mTargetActivity,
					new String[] { "publish_stream, user_photos, email, offline_access" },
					AUTHORIZE_ACTIVITY_RESULT_CODE,
					listener);
			/**/
			mFacebook.authorize2(
							mTargetActivity,
							new String[] { "publish_stream, user_photos, email, offline_access" },
							new AuthorizeListener());
			
		}
	}

	public String getFriendList() {
		String rtn = null;
		try {
			rtn = mFacebook.request("me/friends");

		} catch (Exception e) {

		}
		return rtn;
	}

	public String getMyInfo() {
		String rtn = null;
		try {
			rtn = mFacebook.request("me");

		} catch (Exception e) {

		}
		return rtn;
	}

	public JSONObject getMyInfoToJsonObject() {
		JSONObject json = null;
		try {
			json = new JSONObject(this.getMyInfo());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;

	}

	public JSONObject getFriendListToJsonObject() {
		JSONObject json = null;
		try {
			json = new JSONObject(this.getFriendList());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	/**/
	public class AuthorizeListener implements DialogListener {
		public void onCancel() {
		}

		public void onComplete(Bundle values) {
			if (C.D)
				Log.v(C.LOG_TAG, "::: onComplete :::");

			mFacebookAccessToken = mFacebook.getAccessToken();
			MyPreferences.setAppPreferences(mTargetActivity, "FACEBOOK_TOKEN",
					mFacebookAccessToken);
			try {
				mServer = new MyServer(mTargetActivity, -1);
				JSONObject myInfo = getMyInfoToJsonObject();
				String myInfoStr = getMyInfo();
				Log.v(C.LOG_TAG, myInfoStr);
				if (myInfo.has("id") || myInfo.has("name")) {
					Log.v(C.LOG_TAG, myInfo.getString("id"));
					MyPreferences.setAppPreferences(mTargetActivity,
							"FACEBOOK_USER_ID", myInfo.getString("id"));
					String userKey = mServer.addUser(myInfo.getString("id"),
							myInfo.getString("name"),
							mFacebook.getAccessToken());
					if (userKey.equalsIgnoreCase("0")) { // /< login fail
						Log.v(C.LOG_TAG, "login fail");
					} else {
						MyPreferences.setAppPreferences(mTargetActivity,
								"USER_KEY", userKey);
						Log.v(C.LOG_TAG, "user_key : " + userKey);
						mTargetActivity.finish();
						Intent intent = new Intent(mTargetActivity,
								MainAct.class);
						mTargetActivity.startActivity(intent);
					}
				} else {
					Log.v(C.LOG_TAG, "no id");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public void onError(DialogError e) {
			Log.v(C.LOG_TAG, ": onError : " + e.getMessage());
		}

		public void onFacebookError(FacebookError e) {
			Log.v(C.LOG_TAG, ": face book errorr : " + e.getMessage());

		}
	}	
	/**/	
}
